import {$digest, $init} from '../../lib/page.data'
import {$request, Session} from '../../lib/page.auth'

const {regeneratorRuntime} = global
//获取应用实例
let api = require('../../utils/api.js');
let http = require('../../utils/http.js');
let util = require('../../utils/util.js');
const app = getApp()

// pages/mine/mine.js
Page({

    /**
     * 页面的初始数据
     */
    data:
        {
      showModalStatus: false,
      domain: "https://tutu-yun.oss-cn-shenzhen.aliyuncs.com/",
            user:{},
            tuanzhang:0,
            hehuoren:0
        },
    settingBtn: function () {
        wx.navigateTo({
            url: '/pages/mineSetting/mineSetting',
        })
    },
    async orderSetTap (event) 
    {
        let formId = event.detail.formId;
        await util.getFormIds(formId);

        wx.navigateTo({
            url: '/pages/mineTakeOrder/mineTakeOrder',
        })
    },
    feedback: function () {
        wx.navigateTo({
            url: '/pages/mineFeedback/mineFeedback',
        });
    },
  sharemore: function (e) {
    var that=this;
    var token = wx.getStorageSync('token');
    console.log(this.data.user);
    http.post(api.get_little_code, { token: token, openid: this.data.user.openid }, function (res) {
      console.log(res.data)
      if (res.data.code == 0) {
        wx.showModal(
          {
            title: "提示",
            content: res.data.info,
            showCancel: false
          });
          return;
      }
      else {
        that.setData({
          showModalStatus: true,
          imgurl: api.domain+res.data.data
        })
      }
    }, "请求中...");
    
  },
  hehuoren:function(){
    wx.navigateTo({
      url: '/pages/mineTuan/mineTuan?type=4',
    })
  }, 
  tuanzhang: function() {
    wx.navigateTo({
      url: '/pages/mineTuan/mineTuan?type=3',
    })
  }, 
  daili: function() {
    wx.navigateTo({
      url: '/pages/mineTuan/mineTuan?type=5',
    })
  },
  chakanpeiwan: function () {
    wx.navigateTo({
      url: '/pages/indextuanzhang/indextuanzhang?type=3',
    })
  }, 
  bindCloseTap: function () {
    this.setData({
      showModalStatus: false
    })
  },
    async toOrderTap (event) 
    {
        let formId = event.detail.formId;
        await util.getFormIds(formId);

        wx.navigateTo({
            url: '/pages/mineOrder/mineOrder?uid=' + this.data.user.id,
        });
    },

    walletTap: function () {
        wx.navigateTo({
            url: '/pages/mineWallet/mineWallet',
        });
    },
    skilsRz: function () {
        wx.navigateTo({
            url: '/pages/mineSkillCer/mineSkillCer',
        });
    },
  // 保存二维码到本地
  previewImage: function (e) {
    var current = e.target.dataset.src;
    wx.previewImage({
      current: current,
      urls: [current]
    })
  },
    async getPhoneNumber(e) 
    {
        console.log(e.detail)
        var that = this;
        var token = wx.getStorageSync('token');
        http.post(api.user_mobile, {token:token,data:e.detail}, function(res)
        {
            console.log(res.data)
            if (res.data.code == 0) 
            {
                wx.showModal(
                {
                    title      : "提示",
                    content    : res.data.info,
                    showCancel : false
                });
            }
            else
            {
                //刷新
                // that.data.page = 1;
              console.log(res.data.data)
              that.getPageData();
                // console.log(that.data.skill_list)
                // that.loadMore();
            }
        }, "请求中...");return;

        const res = await $request({url: api.user_mobile, data: e.detail, method: 'POST'})
        if (res.code == 1) {
            wx.showModal({
                title: '提醒',
                showCancel: false,
                content: '手机号码绑定成功！',
                success: function (res) {
                    wx.navigateTo({
                        url: '/pages/mineSkillCer/mineSkillCer',
                    });
                },
            })
        }
        else {
            wx.showModal({
                title: '提醒',
                showCancel: false,
                content: '手机号码绑定失败，请重新操作！',
                success: function (res) {
                },
            })
        }
        console.log(res)

        // const res = await $request({ url: api.skill_list, data: {}, method: 'POST' })
        // http.get(api.user_mobile, {data:e.detail}, function(res)
        // {
        //     let data = JSON.parse(res.data.data);
        //     app.globalData.userInfo.mobile = data;

        //     console.log(data, "获取用户信息接口")
        //     wx.showToast({title : "绑定手机成功", icon : "success"});

        //     setTimeout(function(){
        //         wx.navigateBack({
        //             delta: 1
        //         })
        //     }, 1000);

        // });
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() 
    {
        this.getPageData();
        
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
      var that = this;
      that.onLoad();
        //判断是否有打开过页面
        // if (getCurrentPages().length != 0) {
        //   console.log('我打开过页面');
        //     //刷新当前页面的数据
        //     getCurrentPages()[getCurrentPages().length - 1].onLoad()
        // }
        // if (app.globalData.hasUser) {
        //     this.setData({user: app.globalData.user});
        // }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
      wx.stopPullDownRefresh();
      var that = this;
      that.setData({
        showModalStatus: false,
        domain: "https://tutu-yun.oss-cn-shenzhen.aliyuncs.com/",
        user: {},
        tuanzhang: 0,
        hehuoren: 0,
        daili:0
      })
      this.onLoad(); //重新加载onLoad()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    //获取页面所需数据
    getPageData() 
    {
        var that = this;
        var token = wx.getStorageSync('token');
        http.get(api.user_info, {token:token}, function(res)
        {   
            console.log(res)
            app.globalData.user_id = res.data.data.id
            if (res.data.code == 1) 
            {
              that.setData({ 
                user: res.data.data
              });
              if (res.data.data.utype == 4) {
                that.setData({
                  hehuoren: 1,
                  tuanzhang: 0
                })
              }
              if (res.data.data.utype == 3) {
                that.setData({
                  tuanzhang: 1,
                  hehuoren: 0
                })
              }
              if (res.data.data.utype == 5) {
                that.setData({
                  daili: 1
                })
              }

            }
            else
            {
              util.msg('获取数据失败');
            }
        }, "请求中...");
    },
})